# -*- coding: utf-8 -*-
from teamcity import is_running_under_teamcity
from teamcity.unittestpy import TeamcityTestRunner
import unittest
import sys
import inspect
import importer
from subprocess import call
#import dbus
#_session_bus = dbus.SessionBus()
#_dbus_screensaver = _session_bus.get_object('org.freedesktop.ScreenSaver','/ScreenSaver')
#simulate_activity = _dbus_screensaver.get_dbus_method('SimulateUserActivity','org.freedesktop.ScreenSaver')
#simulate_activity()

print "Coding on start is "+sys.getdefaultencoding()
reload(sys)
sys.setdefaultencoding('utf-8')
print "So we have changed it to "+sys.getdefaultencoding()

try:
    call(['xset', 's', 'off'])
    call(['gsettings', 'set', 'org.gnome.settings-daemon.plugins.power', 'active', 'false'])
except:
    print "Some calls failed"

if __name__ == '__main__':
    if is_running_under_teamcity():
        runner = TeamcityTestRunner()
        print "Run using teamcity"
    else:
        runner = unittest.TextTestRunner()
        print "Run using unittest"
    cases = [obj for name, obj in inspect.getmembers(importer) if not '__' in name]
    i = 0
    suite = unittest.TestSuite()
    for case in cases:
        suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(case))
        #if i//8:
        runner.run(suite)
        suite = unittest.TestSuite()
        #else: i+=1

try:
    call(['xset', 's', 'on'])
    call(['gsettings', 'set', 'org.gnome.settings-daemon.plugins.power', 'active', 'true'])
except:
    print "Some calls failed"
